package org.com.yz.dubbo.service;

import org.com.yz.common.constant.PayConstant;
import org.com.yz.common.util.DateUtil;
import org.com.yz.common.util.ToolUtil;
import org.com.yz.dal.dao.mapper.*;
import org.com.yz.dal.dao.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author: dingzhiwei
 * @date: 17/9/9
 * @description:
 */
@Service
public class BaseService {

    @Autowired
    private PayChannelMapper payChannelMapper;

    @Autowired
    private MchNotifyMapper mchNotifyMapper;

    @Autowired
    private MchAccntMapper mchAccntMapper;

    @Autowired
    private HisAccntMchSubMapper hisAccntMchSubMapper;

    @Autowired
    private HisAccntOnwayMapper hisAccntOnwayMapper;

    @Autowired
    private BankCardMapper bankCardMapper;

    @Autowired
    private DepositTransactionMapper depositTransactionMapper;

    @Autowired
    private HisAccntMchMapper hisAccntMchMapper;

    @Autowired
    private HisAccntOnway2Mapper hisAccntOnway2Mapper;

    @Autowired
    private MchAccntChargeUpMapper mchAccntChargeUpMapper;

    @Autowired
    private HisAccntProfileMapper hisAccntProfileMapper;

    @Autowired
    private HisAccntSupplierAssMapper hisAccntSupplierAssMapper;

    @Autowired
    private WithDrawInfoMapper withDrawInfoMapper;

    @Autowired
    private HisAccntPrepayMapper hisAccntPrepayMapper;

    @Autowired
    private TrunListPageMapper trunListPageMapper;

    @Autowired
    private ErrorTransactionDetailsMapper errorTransactionDetailsMapper;





    public MchAccnt baseSelectMchAccnt(MchAccnt mchAccnt){
        List<MchAccnt> list = mchAccntMapper.selectMchAccnt(mchAccnt);
        if(list.size()>0 && list.get(0)!=null){
            return  list.get(0);
        }
        return  null;
    }

    /**
     * 插入子商户资金交易明细
     * @param hisAccntMchSub
     * @return
     */
    public int insertHisMchASub(HisAccntMchSub hisAccntMchSub){
        return hisAccntMchSubMapper.insert(hisAccntMchSub);
    }

    /**
     * 插入存管户交易明细
     * @return
     */
    public int insertHisMchAccnt(HisAccntMch hisMchAccnt){
        return hisAccntMchMapper.insert(hisMchAccnt);
    }

    /**
     * 插入分润帐户交易明细
     * @param hisAccntProfile
     */
    public int insertHisProfile(HisAccntProfile hisAccntProfile) {
        return hisAccntProfileMapper.insert(hisAccntProfile);
    }
    /**
     * 插入子在途账户表
     * @return
     */
    public int insertHisOnway(HisAccntOnway hisAccntOnway){
        return hisAccntOnwayMapper.insert(hisAccntOnway);
    }

    /**
     * 插入提现记录表
     */
    public int insetWithDraw(WithDrawInfo withDrawInfo){
        return  withDrawInfoMapper.insert(withDrawInfo);
    }

    /**
     * 查询在途总额信息
     * @param
     * @return
     */
    public HisAccntOnway selectHisAccntOnway(HisAccntOnway hisAccntOnway){
        List<HisAccntOnway> list = hisAccntOnwayMapper.selectHisAccntOnway(hisAccntOnway);
        if(list.size()>0 && list.get(0)!=null){
            return  list.get(0);
        }
        return  null;
    }


    /**
     *查询在途2账户信息
     */
    public HisAccntOnway2 selectHisAccntOnway2(HisAccntOnway2 hisAccntOnway2){
        List<HisAccntOnway2> list = hisAccntOnway2Mapper.selectHisAccntOnway2(hisAccntOnway2);
        if(list.size()>0 && list.get(0)!=null){
            return  list.get(0);
        }
        return  null;
    }
    /**
     * 查询还准备金
     * @param mchAccntChargeUp
     * @return
     */
    public MchAccntChargeUp selectMchAccntChargeUp(MchAccntChargeUp mchAccntChargeUp){
        List<MchAccntChargeUp> list = mchAccntChargeUpMapper.selectMchAccntChargeUp(mchAccntChargeUp);
        if(list.size()>0 && list.get(0)!=null){
            return  list.get(0);
        }
        return  null;
    }
    /**
     * 插入还准备金交易记录表
     * @return
     */
    public void insertMchAccntChargeUp(MchAccntChargeUp mchAccntChargeUp){
        mchAccntChargeUpMapper.insert(mchAccntChargeUp);
    }

    /**
     * 插入准备金记录表
     */
    public void insertHisPrepay(HisAccntPrepay hisAccntPrepay){
        hisAccntPrepayMapper.insert(hisAccntPrepay);
    }

    /**
     * 更新还准备金
     */
    public void updateMchAcchtChargeUp(MchAccntChargeUp mchAccntChargeUp){
        mchAccntChargeUpMapper.updateChargeUp(mchAccntChargeUp);
    }

    /**
     * 更新还准备金
     */
    public void updateMchAcchtChargeUpAmt(MchAccntChargeUp mchAccntChargeUp){
        mchAccntChargeUpMapper.updateChargeUpAmt(mchAccntChargeUp);
    }

    /**
     * 更新账户余额
     * @return
     */
    public int updateMchAccntAmt(MchAccnt mchAccnt){
        return  mchAccntMapper.updateAccmount(mchAccnt);
    }

    /**
     * 更新账户余额
     * @return
     */
    public int updateMchAmt(MchAccnt mchAccnt){
        return  mchAccntMapper.updateMchAmt(mchAccnt);
    }

    public int updateMchAmtNoZero(MchAccnt mchAccnt){
        return  mchAccntMapper.updateMchAmtNoZero(mchAccnt);
    }

    /**
     *查询银行卡相关信息
     * @return
     */
    public BankCard selectBankCard(String mch_no,String card_no,String mchAccntNo,String accnt_type ){
        return  bankCardMapper.selectBankCardInfo(mch_no,card_no,mchAccntNo,accnt_type);
    }

    /**
     * 查询电商平台账户体系信息
     */
    public Map selectAllAccntInfo(String mchNo,String mchAccntNo,String orderNo){

        MchAccnt accnt = new MchAccnt();
        accnt.setMchNo(mchNo);//商户号
        accnt.setAccntType("depository");//存管户
        MchAccnt accnt1 = baseSelectMchAccnt(accnt);//存管户账户信息（交易前）
        MchAccnt accnt2 = new MchAccnt();
        accnt2.setMchNo(mchNo);//商户号
        accnt2.setAccntType("prepay");//准备金
        MchAccnt accnt3 = baseSelectMchAccnt(accnt2);//准备金账户信息（交易前）

        MchAccnt accnt4 = new MchAccnt();
        accnt4.setMchNo(mchNo);//商户号
        accnt4.setAccntType("countamt");//商户可用总余额
        MchAccnt accnt5 = baseSelectMchAccnt(accnt4);//商户可用总余额账户（交易前）

       /* //商户可用总余额信息
        MchAccnt profitAccnt = new MchAccnt();
        accnt.setMch_no(mchNo);//
        accnt.setAccnt_type("profit");//
        MchAccnt profitAccntInfo = baseSelectMchAccnt(accnt);//分润账户余额信息

        HisAccntOnway hisAccntOnway = new HisAccntOnway();
        hisAccntOnway.setType("count");
        hisAccntOnway.setMch_no(mchNo);
        HisAccntOnway hisAccntOnwayInfo =selectHisAccntOnway(hisAccntOnway);//在途1账户总额*/

        HisAccntOnway2 hisAccntOnway2 = new HisAccntOnway2();
        hisAccntOnway2.setType("count");
        hisAccntOnway2.setMchNo(mchNo);
        HisAccntOnway2 hisAccntOnway2Info = selectHisAccntOnway2(hisAccntOnway2);//在途2账户总额



        MchAccntChargeUp mchAccntChargeUp = new MchAccntChargeUp();
        mchAccntChargeUp.setMchNo(mchNo);
        mchAccntChargeUp.setType("count");
        MchAccntChargeUp mchAccntChargeUpInfo = selectMchAccntChargeUp(mchAccntChargeUp);//还准备金账户总额



        MchAccnt accnt_ = new MchAccnt();
        accnt_.setMchNo(mchNo);
        accnt_.setMchAccntNo(mchAccntNo);
        //查询提现账户的账户信息
        MchAccnt withDrawMch = baseSelectMchAccnt(accnt_);

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("withDraw",withDrawMch);
        resultMap.put("depository",accnt1);
        resultMap.put("prepay",accnt3);
        /*resultMap.put("profit",profitAccntInfo);
        resultMap.put("onway",hisAccntOnwayInfo);*/
        resultMap.put("onway2",hisAccntOnway2Info);
        resultMap.put("chargeup",mchAccntChargeUpInfo);
        resultMap.put("countamt",accnt5);
        return  resultMap;
    }

    public HisAccntPrepay selectHisAccntPrepay(HisAccntPrepay hisAccntPrepay) {
        return hisAccntPrepayMapper.selectHisAccntPrepay(hisAccntPrepay);
    }

    public List<HisAccntPrepay> selectHisAccntPrepayList(HisAccntPrepay hisAccntPrepay) {
        return hisAccntPrepayMapper.select(hisAccntPrepay);
    }

    /**
     * 插入事务表
     * @return
     */
    public int insetDeposiTranction(DepositTransaction depositTransaction){
        return depositTransactionMapper.insert(depositTransaction);
    }

    /**
     * 插入在途2账户表
     * @param hisAccntOnway2
     */
    public void insertHisAccntOnway2(HisAccntOnway2 hisAccntOnway2){
        hisAccntOnway2Mapper.insert(hisAccntOnway2);
    }
    /**
     * 更新在途账户2信息
     * @param hisAccntOnway2
     */
    public void updateHisAccntOnway2(HisAccntOnway2 hisAccntOnway2){
        hisAccntOnway2Mapper.updateOnway2(hisAccntOnway2);
    }

    /**
     * 更新在途账户信息
     * @param
     */
    public int updateHisAccntOnway(HisAccntOnway hisAccntOnway){
       return  hisAccntOnwayMapper.updateOnway(hisAccntOnway);
    }


    public List<HisAccntMchSub> selectHisAccntMchSub(String mchNo,String orderNo){
        return hisAccntMchSubMapper.selectHisAccntMchSubInfo(mchNo,orderNo);
    }

    public List<HisAccntMchSub> selectHisAccntMchSubByTransNo(String mchNo,String transNo){
        return hisAccntMchSubMapper.selectHisAccntMchSubInfoByTransNo(mchNo,transNo);
    }



    public List<HisAccntProfile> selectHisAccntProfile(String mchNo,String orderNo){
        return hisAccntProfileMapper.selectHisAccntProfile(mchNo,orderNo);
    }












    public PayChannel baseSelectPayChannel(String mchId, String channelId) {
        PayChannelExample example = new PayChannelExample();
        PayChannelExample.Criteria criteria = example.createCriteria();
        criteria.andChannelIdEqualTo(channelId);
        criteria.andMchIdEqualTo(mchId);
        List<PayChannel> payChannelList = payChannelMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(payChannelList)) return null;
        return payChannelList.get(0);
    }

    public MchNotify baseSelectMchNotify(String orderId) {
        return mchNotifyMapper.selectByPrimaryKey(orderId);
    }

    public int baseInsertMchNotify(String orderId, String mchId, String mchOrderNo, String orderType, String notifyUrl) {
        MchNotify mchNotify = new MchNotify();
        mchNotify.setOrderId(orderId);
        mchNotify.setMchId(mchId);
        mchNotify.setMchOrderNo(mchOrderNo);
        mchNotify.setOrderType(orderType);
        mchNotify.setNotifyUrl(notifyUrl);
        return mchNotifyMapper.insertSelectiveOnDuplicateKeyUpdate(mchNotify);
    }

    public int baseUpdateMchNotifySuccess(String orderId, String result, byte notifyCount) {
        MchNotify mchNotify = new MchNotify();
        mchNotify.setStatus(PayConstant.MCH_NOTIFY_STATUS_SUCCESS);
        mchNotify.setResult(result);
        mchNotify.setNotifyCount(notifyCount);
        mchNotify.setLastNotifyTime(new Date());
        MchNotifyExample example = new MchNotifyExample();
        MchNotifyExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        List values = new LinkedList<>();
        values.add(PayConstant.MCH_NOTIFY_STATUS_NOTIFYING);
        values.add(PayConstant.MCH_NOTIFY_STATUS_FAIL);
        criteria.andStatusIn(values);
        return mchNotifyMapper.updateByExampleSelective(mchNotify, example);
    }

    public int baseUpdateMchNotifyFail(String orderId, String result, byte notifyCount) {
        MchNotify mchNotify = new MchNotify();
        mchNotify.setStatus(PayConstant.MCH_NOTIFY_STATUS_FAIL);
        mchNotify.setResult(result);
        mchNotify.setNotifyCount(notifyCount);
        mchNotify.setLastNotifyTime(new Date());
        MchNotifyExample example = new MchNotifyExample();
        MchNotifyExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        List values = new LinkedList<>();
        values.add(PayConstant.MCH_NOTIFY_STATUS_NOTIFYING);
        values.add(PayConstant.MCH_NOTIFY_STATUS_FAIL);
        return mchNotifyMapper.updateByExampleSelective(mchNotify, example);
    }

    /**
     * 查询提现接口返回所需实体
     * @param orderNo
     * @param mchAccntNo
     * @param mchNo
     * @return
     */
    public WithDraw checkWithDraw(String orderNo, String mchAccntNo, String mchNo) {
        return  withDrawInfoMapper.selectWithDraw(orderNo,mchAccntNo,mchNo);
    }

    public BankCardBin getBankCardBinByBankName(String bankcardBin) {
        return  bankCardMapper.getBankCardBinByCardBin(bankcardBin);
    }

    public List<WithDrawInfo> selectWithdrawInfo(WithDrawInfo withDrawInfo) {
        return withDrawInfoMapper.select(withDrawInfo);
    }

    public int updateWithDrawInfo(WithDrawInfo withDrawInfo) {
        return withDrawInfoMapper.updateWithDrawInfo(withDrawInfo);
    }

    public TrunListPage selectTrunListPageInfo(String mchNo,String lastDate ,String status) {
            TrunListPage trunListPage = new TrunListPage();
            trunListPage.setMchNo(mchNo);
            trunListPage.setRecordDate(lastDate);
            trunListPage.setStatus(status);
            List<TrunListPage> trunListPageList = trunListPageMapper.selectTrunListPageInfo(trunListPage);
            if(ToolUtil.isNotEmpty(trunListPageList)){
                return trunListPageList.get(0);
            }
            return null ;
    }

    public void updateTrunListPageInfo(String mchNo, String lastDate) {
        TrunListPage trunListPage = new TrunListPage();
        trunListPage.setMchNo(mchNo);
        trunListPage.setRecordDate(lastDate);
        trunListPage.setStatus("1");
        trunListPageMapper.updateTrunListPageInfo(trunListPage);
    }

    public void updateTrunListPageInfo(String mchNo, String lastDate, String page2) {
        TrunListPage trunListPage = new TrunListPage();
        trunListPage.setMchNo(mchNo);
        trunListPage.setRecordDate(lastDate);
        trunListPage.setPage(page2);
        trunListPageMapper.updateTrunListPage(trunListPage);
    }

    public void insertTrunListPage(String mchNo, String day, String status) {
        TrunListPage trunListPage = new TrunListPage();
        trunListPage.setMchNo(mchNo);
        trunListPage.setRecordDate(day);
        trunListPage.setStatus(status);
        trunListPage.setPage("1");
        trunListPage.setCreateTime(new Date());
        trunListPageMapper.insert(trunListPage);
    }



    public int insertErrorTransactionDetail(ErrorTransactionDetails errorTransactionDetails) {
        return  errorTransactionDetailsMapper.insert(errorTransactionDetails);
    }

    public ErrorTransactionDetails selectErrorTransactionDetail(String mchNo, String attachinfo) {
        ErrorTransactionDetails errorTransactionDetails = new ErrorTransactionDetails();
        errorTransactionDetails.setMchno(mchNo);
        errorTransactionDetails.setAttachinfo(attachinfo);
        List<ErrorTransactionDetails> errorTransactionDetailsList = errorTransactionDetailsMapper.selectErrorTransactionDetails(errorTransactionDetails);
        if(errorTransactionDetailsList.size()>0){
            return  errorTransactionDetailsList.get(0);
        }
        return null;
    }

    public List<WithDrawInfo> selectWithDrawInfoByDate(WithDrawInfo withDrawInfo) {
        return withDrawInfoMapper.selectWithDrawInfoByDate(withDrawInfo);
    }

    /**
     * 插入待结算供应商子账户资金明细
     * @param hisAccntSupplierAss
     * @return
     */
    public int insertHisSupplierAss(HisAccntSupplierAssurance hisAccntSupplierAss) {
        return hisAccntSupplierAssMapper.insert(hisAccntSupplierAss);

    }

    public List<HisAccntSupplierAssurance> selectHisAccntSupplierAss(String mchNo, String orderNo) {
        return hisAccntSupplierAssMapper.selectHisAccntSupplierAss(mchNo,orderNo);
    }
}
